package com.say.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import com.say.bean.domain.Chat;
import com.say.bean.domain.Message;
import com.say.dao.BaseQueryDao;
import com.say.dao.ChatDao;
import com.say.framework.core.common.service.BaseServiceImpl;
import com.say.service.ChatService;
import com.say.service.MessageService;
import com.say.service.UserService;
import com.say.service.WordsService;


/**
 * 
 * 类名称：ConfigServiceImpl<br>
 * 类描述：系统参数服务接口实现<br>
 * @version v1.0
 *
 */
@Service("chatService")
public class ChatServiceImpl extends BaseServiceImpl<Chat, String> implements ChatService {

    @Autowired
    public ChatDao chatDao;
    
    @Autowired
    public BaseQueryDao baseQueryDao;

	@Autowired
	protected UserService userService;
	
	@Autowired
	protected MessageService messageService;
	
	@Autowired
	public WordsService wordsService;
	 
    @Override
    public PagingAndSortingRepository<Chat, String> getDao() {
        return chatDao;
    }

	@Override
	public Page<Map<String, Object>> findChatByPage(Map<String, Object> params, Pageable initPage) {
		return baseQueryDao.findChatByPage(params, initPage);
	}

	@Override
	public List<Chat> findChatByIntervalPagePage(String userId,String toUserId, String maxChatId) {
		return chatDao.findChatByIntervalPagePage(userId,toUserId, maxChatId);
	}

	@Override
	public void updateChatStautsByUser(String userId) {
		chatDao.updateChatStautsByUser(userId);
	}

	@Override
	public void send(String userId,String toUserId,Integer type,String text) {
		try {
			Chat chat=new Chat();
			chat.setUserId(userId);
			chat.setToUserId(toUserId);
			chat.setType(type);
			chat.setText(text);
			chat.setStatus(1);
			chat.setCreateTime(new Date());
			this.save(chat);
		
			Message message=messageService.getMessageByUserIdAndToUserId(userId, toUserId);
			if(message!=null) {
				messageService.saveUnReadMessageById(message.getId(), message.getUnRead()+1, text);
			}else {
				message=new Message();
				message.setStatus(1);
				message.setUserId(userId);
				message.setToUserId(toUserId);
				message.setText(text);
				message.setUnRead(1);
				message.setUpdateTime(new Date());
				messageService.save(message);
			}
			
			message=messageService.getMessageByUserIdAndToUserId(toUserId, userId);
			if(message!=null) {
				messageService.saveUnReadMessageById(message.getId(), message.getUnRead(), text);
			}else {
				message=new Message();
				message.setStatus(1);
				message.setUserId(toUserId);
				message.setToUserId(userId);
				message.setText(text);
				message.setUnRead(0);
				message.setUpdateTime(new Date());
				messageService.save(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

   

	
}
